﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace hDUniverseSimulator.AspMvcUtils
{
    /// <summary>
    /// Defines the available drawers for the hDUniverseSimulator
    /// </summary>
    public enum hDUniverseSimulator_Drawer
    {
        Default,
        UniverseEntityDrawBase,
        InPageHtmlFixtureDrawer,
        MetroStyleDrawer
    }

    /// <summary>
    /// Defines HtmlHelper extension methods
    /// </summary>
    public static class HtmlHelperExtensions
    {
        /// <summary>
        /// Writes the JS and CSS includes for the hDUniverseSimulator
        /// </summary>
        /// <param name="htmlHelper">htmlHelper</param>
        /// <param name="basePath">hDUniverseSimulator base path</param>
        /// <param name="drawer">hDUniverseSimulator drawer type</param>
        /// <returns>HTML code with the required references to be put in the page's &lt;head&gt; section</returns>
        public static IHtmlString hDUniverseSimulator_JsAndCssIncludes(this HtmlHelper htmlHelper, string basePath, hDUniverseSimulator_Drawer drawer, Action<IEnumerable<string>> cssReferencesHandler, Action<IEnumerable<string>> jsReferencesHandler)
        {
            List<string> includesCss = new List<string>(new string[] { 
                "css/styles.css"
            });

            List<string> includesJs = new List<string>(new string[] { 
                "Common/Align.js",
                "Common/Point.js",
                "Common/Size.js",
                "Common/Rectangle.js",
                "Common/Range.js",
                "Common/Vector.js",

                "UniverseBL/UniverseEntity.js",
                "UniverseBL/Planet.js",
                "UniverseBL/Star.js",
                "UniverseBL/SolarSystem.js",
                "UniverseBL/Galaxy.js",
                "UniverseBL/StarField.js",
                "UniverseBL/ShootingStar.js",
                "UniverseBL/Universe.js",

                "Physics/BasicPhysics.js",

                "Drawing/Animation/AnimationBase.js",
                "Drawing/Animation/MetroStyleAnimation.js",

                "Drawing/UniverseEntityDrawBase.js",
                "Drawing/InPageHtmlFixtureDrawer.js",
                "Drawing/MetroStyleDrawer.js",
                "Drawing/DrawingUtility.js",

                "UniverseFixture/BaseFixture.js",
                "UniverseFixture/InPageHtmlFixture.js"
            });
            switch (drawer)
            {
                case hDUniverseSimulator_Drawer.MetroStyleDrawer:
                    includesCss.Add("css/styles_metro.css");
                    break;
            }

            if (cssReferencesHandler != null)
                cssReferencesHandler(includesCss.Select(i => basePath + "/" + i).ToList());
            if (jsReferencesHandler != null)
                jsReferencesHandler(includesJs.Select(i => basePath + "/" + i).ToList());

            UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);
            basePath = urlHelper.Content(basePath);

            StringBuilder stringBuilder = new StringBuilder();

            if(cssReferencesHandler == null)
                includesCss.ForEach(i => {
                    TagBuilder tagBuilder = new TagBuilder("link");
                    tagBuilder.Attributes.Add("href", basePath + "/" + i);
                    tagBuilder.Attributes.Add("rel", "stylesheet");
                    tagBuilder.Attributes.Add("type", "text/css");
                    stringBuilder.AppendLine(tagBuilder.ToString(TagRenderMode.SelfClosing));
                });
            if(jsReferencesHandler == null)
                includesJs.ForEach(i =>
                {
                    TagBuilder tagBuilder = new TagBuilder("script");
                    tagBuilder.Attributes.Add("src", basePath + "/" + i);
                    tagBuilder.Attributes.Add("type", "text/javascript");
                    stringBuilder.AppendLine(tagBuilder.ToString(TagRenderMode.Normal));
                });

            TagBuilder tagBuilderMarkIncludes = new TagBuilder("script");
            tagBuilderMarkIncludes.Attributes.Add("type", "text/javascript");
            tagBuilderMarkIncludes.InnerHtml = "var hDUniverseSimulator_AreJsAndCssIncluded = true;";
            stringBuilder.AppendLine(tagBuilderMarkIncludes.ToString(TagRenderMode.Normal));

            return htmlHelper.Raw(stringBuilder.ToString());
        }
    }
}